import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
需翻墙
scrapy crawl tempac -o tempac.json
https://dhfde-000.av6k519.top/rihanwuma/
'''

class TempacSpider(scrapy.Spider):
    name = "tempac"
    # 前缀
    prefix = 'https://dhfde-000.'
    # 中缀
    website = 'av6k519'
    # 后缀
    suffix = '.top/'
    allowed_domains = [website + '.top']
    # start_urls = [prefix + website + suffix+'rihanwuma/']
    start_urls = ['https://dhfde-000.av6k519.top/rihanwuma/play/p104890.html']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        tags = response.xpath("//div[@class='title']//a/text()").extract()
        url = response.xpath("//div[@class='detail-img']//a/@ href").extract()
        p_url = response.xpath("//div[@class='detail-img']//a//img/@ src").extract()
        name = response.xpath("//div[@class='detail-img']//a//img/@ title").extract()
        if len(url) and len(p_url) and len(name) and len(tags):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[-1], url=split_joint(self.prefix + self.website + self.suffix, url[0]), name=name[0], pUrl=split_joint(self.prefix + self.website + self.suffix, p_url[0]), vUrl='')
        if len(video_url):
            unique_list = list(set(video_url))
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=format_url_one(unique_list[0]))
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)